package master;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合
 *
 * @author chenyw
 * @date 2022/7/28 20:01
 */
public class M77combine {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        int[] visited = new int[n +1];
        dfs(n, k, new ArrayList<Integer>(), visited, 1);
        return res;
    }

    private void dfs(int n, int k, ArrayList<Integer> list, int[] visited, int start) {
        if (list.size() == k) {
            res.add(new ArrayList<>(list));
        }
        for (int i = start; i <= n; i++) {
            if (visited[i] == 1) {
                continue;
            }
            visited[i] = 1;
            list.add(i);
            dfs(n, k, list, visited, i + 1);
            visited[i] = 0;
            list.remove(list.size() - 1);
        }
    }
}
